/*
 * Copyright (c) 2018. czmc All Rights Reserved.
 * Author: czmc
 * Email: zxc6355533@163.com
 * Encoding: UTF-8
 */

package top.stime21.myneighbours.module.details

import android.content.Intent
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_need_details.*
import kotlinx.android.synthetic.main.include_header.*
import top.stime21.framework.extension.toTime
import top.stime21.myneighbours.R
import top.stime21.myneighbours.api.LocalDataManager
import top.stime21.myneighbours.base.BuzMvpActivity
import top.stime21.myneighbours.base.showConfirmDialog
import top.stime21.myneighbours.data.entity.Need
import top.stime21.myneighbours.data.entity.User
import top.stime21.myneighbours.module.login.NeedContract
import top.stime21.myneighbours.module.login.presenter.NeedPrensenterImpl
import top.stime21.myneighbours.utils.ImageLoader

class NeedDetailsActivity : BuzMvpActivity<NeedContract.Presenter>(), NeedContract.View {
    private var user: User?=null

    override fun onDetailsGetSuccess(data: Need?) {
        if(data!=null) need =data
        when (need!!.status) {
            1 -> {
                view_opera.visibility = View.VISIBLE
                tv_status.setTextColor(resources.getColor(R.color.colorWaitting))
                tv_status.setText("等待中")
                btn_accept.setText("接受")
                btn_accept.setBackgroundColor(resources.getColor(R.color.colorWaitting))
                btn_accept.setOnClickListener {
                    showConfirmDialog("提示", "是否确认接受此需求", View.OnClickListener {
                        mPrensenter!!.accept(need!!.id)
                    });
                }
                btn_back_see.setOnClickListener { finish() }
            }
            2 -> {
                if(user !=null && need!!.accepter!=null && need!!.accepter.userId== user!!.userId){
                    btn_accept.visibility = View.GONE
                    btn_back_see.setText("取消当前需求的赴约")
                }else{
                    btn_back_see.setText("取消")
                }
                view_opera.visibility = View.VISIBLE
                tv_status.setTextColor(resources.getColor(R.color.colorDoing))
                tv_status.setText("进行中")
                btn_back_see.setTextColor(Color.WHITE)
                btn_back_see.setBackgroundColor(resources.getColor(R.color.colorCancel))
                btn_back_see.setOnClickListener {
                    showConfirmDialog("提示", "是否取消此次行动", View.OnClickListener {
                        mPrensenter!!.cancel(need!!.id)
                    });
                }
                btn_accept.setText("完成")
                btn_accept.setBackgroundColor(resources.getColor(R.color.colorDoing))
                btn_accept.setOnClickListener {
                    showConfirmDialog("提示", "是否确定完成", View.OnClickListener {
                        mPrensenter!!.finish(need!!.id)
                    });
                }
            }
            3 -> {
                view_opera.visibility = View.GONE
                tv_status.setText("已完成")
                tv_status.setTextColor(resources.getColor(R.color.colorFinish))
            }
            4,5-> {
                view_opera.visibility = View.GONE
                tv_status.setText("已取消")
                tv_status.setTextColor(resources.getColor(R.color.colorCancel))
            }
        }
        if(user ==null || need!!.user!!.userId == user!!.userId){
            view_opera.visibility = View.GONE
        }
        if(need!!.accepter!= null){
            view_accepter.visibility = View.VISIBLE
            tv_name_accept.text = need!!.accepter.nickName
            tv_phone_accept.text = need!!.accepter.phone
            if(need!!.accepter.avatar!=null){
                ImageLoader().loadCircle(need!!.accepter.avatar,iv_avatar_accept)
            }else{
                when (need!!.accepter.gender){
                    1->iv_avatar_accept.setImageResource(R.drawable.ic_male)
                    2->iv_avatar_accept.setImageResource(R.drawable.ic_female)
                }
            }
        }else{
            view_accepter.visibility = View.GONE
        }
        tv_phone.setOnClickListener {
            val intent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + tv_phone.text.toString()))
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            startActivity(intent)
        }
        tv_date.setText(need!!.createTime.toTime())
        tv_name.setText(need!!.user.nickName)
        tv_signature.setText(need!!.user.signature)
        tv_need_title.setText(need!!.title)
        tv_phone.setText(need!!.user.phone)
        tv_content.setText(need!!.content)
        tv_phone_accept.setOnClickListener({
            val intent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + tv_phone_accept.text.toString()))
            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
            startActivity(intent)
        })
        if(need!!.user.avatar!=null){
            ImageLoader().loadCircle(need!!.user.avatar,iv_avatar)
        }
    }

    override fun onFinishSuccess() {
        showConfirmDialog("温馨提示","需求已完成", View.OnClickListener {initData() })
    }

    override fun onMyNeedGetSuccess(data: List<Need>?) {
    }

    override fun onMyNeedMoreGetSuccess(data: List<Need>?) {
    }

    private var need: Need? = null
    override fun onPublishSuccess() {
    }

    override fun onAcceptSuccess() {
        showConfirmDialog("温馨提示","需求已被接受", View.OnClickListener { initData() })
    }

    override fun getPresenter(): NeedContract.Presenter {
        return NeedPrensenterImpl(this)
    }

    override fun onCancleSuccess() {
        showConfirmDialog("温馨提示","需求已被取消", View.OnClickListener { initData() })
    }

    override fun onNearbySuccess(data: List<Need>?) {
    }

    override fun onSearchSuccess(data: List<Need>?) {
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_need_details)
        vLoading!!.setRetryListener {   initData() }
        tv_title.setText("需求详情")
        initData()
    }



    fun initData() {
         user = LocalDataManager.ins().getUser(this)
        var data = intent.getSerializableExtra("data")
        if (data == null) {
            finish()
        }
        need = data as Need
        mPrensenter!!.details(need!!.id)
    }

    override fun showEmpty() {
        vLoading!!.showEmpty()
    }

    override fun showContent() {
        vLoading!!.showContent()
    }

    override fun showError() {
        vLoading!!.showError()
    }

    override fun showViewLoadding() {
        vLoading!!.showLoading()
    }
}
